/*
package com.spsy.search.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spsy.search.entity.SpsyProject;
import com.spsy.search.service.ElasticSearchService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

*/
/**
 * @author: liuzhiping
 * @date: 2024/11/28 16:59
 **//*

@RestController
@RequestMapping("/search")
public class EsController {


    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;


    //第一阶段实现
    @Autowired
    private RestHighLevelClient restHighLevelClient;


    //第二阶段实现
    @Autowired
    private ElasticSearchService elasticSearchService;

    */
/***
     * 创建索引
     * @return
     *//*

    @PostMapping("/create")
    public String create(){

        IndexOperations indexOperations = this.elasticsearchRestTemplate.indexOps(SpsyProject.class);

        boolean exists = indexOperations.exists();

        if(exists){
            return "fail";
        }
        boolean boo = indexOperations.create();
        Document document = indexOperations.createMapping(SpsyProject.class);
        indexOperations.putMapping(document);

       return  "success";
    }


    @PostMapping ("/add")
    public String add() throws Exception{

        SpsyProject spsyProject = new SpsyProject(new Integer(12),"苹果手机12",new Double(6900.00));

        System.out.println("spsyProject:"+ JSON.toJSONString(spsyProject));

        //调用添加
        return this.elasticSearchService.add("spsy_project4","12",spsyProject);

        //第一阶段最原始的实现

        */
/*//*
/构建数据
        SpsyProject spsyProject = new SpsyProject(new Integer(2),"苹果手机",new Double(6900.00));
        Map map = new ObjectMapper().convertValue(spsyProject,Map.class);
        IndexRequest indexRequest = new IndexRequest("spsy_project3");
        indexRequest.id("5");
        indexRequest.source(map);

        //往elasticSearch 索引sysy_project3中添加数据
        IndexResponse indexResponse = this.restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);
        System.out.println("result334445555:"+indexResponse.status().getStatus());*//*



        //elasticsearchRestTemplate.save(spsyProject);

    }


    @PostMapping("/del/{id}")
    public String del(@PathVariable Integer id){
        System.out.println("删除 id:"+id);
        this.elasticSearchService.del("spsy_project4",String.valueOf(id));
        return  "success";
    }



    @PostMapping ("/testadd")
    public String testadd() throws Exception{

        //第一阶段最原始的实现
        //构建数据
        SpsyProject spsyProject = new SpsyProject(new Integer(10),"苹果手机",new Double(6900.00));
        Map map = new ObjectMapper().convertValue(spsyProject,Map.class);
        IndexRequest indexRequest = new IndexRequest("spsy_project4");
        indexRequest.id("10");
        indexRequest.source(map);
        //往elasticSearch 索引sysy_project3中添加数据
        IndexResponse indexResponse = this.restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);
        System.out.println("result334445555777:"+indexResponse.status().getStatus());

        Thread.sleep(3000);

        return "success";

    }

    @GetMapping ("/get/{id}")
    public String query(@PathVariable Integer id) throws Exception{

           SpsyProject spsyProject =  this.elasticSearchService.getById("spsy_project4",String.valueOf(id));

           return  JSON.toJSONString(spsyProject);
    }

}
*/
